<?php
/**
 * fly
 * Copyright By 5920Team (http://www.5920.me)
 * User: yu@5920.me
 * Date: 13-9-30
 * Time: 下午3:16
 */


class Template
{
    private $conf;



    private $templatePath;

    private $cachePath;

    private $templateExtension;

    private $cacheExtension;

    private $cacheSwitch;

    private $data;


    private $reg;

    private $l; //左标签
    private $r; //右标签

    // template 语法
    private $cL = '<!--';
    private $cR = '-->';

    private function render()
    {

    }

    private function format($str)
    {
        $str = str_replace('{','/{',$str);
        return $str;
    }

    public function __construct(array $config)
    {
        $this->l = isset($config['tagLeft']) ? $config['tagLeft'] : '{';
        $this->r = isset($config['tagRight']) ? $config['tagRight'] : '}';

        $this->templatePath = $config['path'];
        $this->cachePath = $config['cache_path'];
        $this->templateExtension = $config['extension'];
        $this->cacheExtension = $config['cache_extension'];

        $usrReg = isset($config['usrReg']) ? $config['usrReg'] : array();

        $varReg = array(
            /**
             * {var}
             * <?php echo $var?>
             */
            '|' . $this->l . '([a-z0-9_]+)' . $this->r . '|i'
            =>
            '<?php echo \$$1?>',

            /**
             * {arr.key}
             * <?php echo $arr[key]?>
             */
            '|' . $this->l . '([a-z0-9_]+)\.([a-z0-9_]+)' . $this->r . '|i'
            =>
            '<?php echo \$$1[\'$2\']?>',

            /**
             * {arr.key1.key2}
             * <?php echo $arr[key1][key2]?>
             */
            '|' . $this->l . '([a-z0-9_]+)\.([a-z0-9_]+)\.([a-z0-9_]+)' . $this->r . '|i'
            =>
            '<?php echo \$$1[\'$2\'][\'$3\']?>',

            /**
             * {.if val}HTML{.end}
             * <?php if($val){ ?>HTML<?php } ?>
             */
            '|' . $this->cL . $this->l . '\.if\s+([a-z0-9_]+)' . $this->r . $this->cR . '([\w\W]*?)' . $this->cL . $this->l . '\.end' . $this->r . $this->cR . '|i'
            =>
            '<?php if(\$$1){ ?>$2<?php } ?>',

            /**
             * {.val?}HTML1{:}HTML2{.end}
             * <?php if($val){ ?>HTML1<?php }else{ ?>HTML2<?php } ?>
             */
            '|' . $this->cL . $this->l . '\.([a-z0-9_]+)\?' . $this->r . $this->cR . '([\w\W]*?)' . $this->cL . $this->l . '\:' . $this->r . $this->cR . '([\w\W]*?)' . $this->cL . $this->l . '\.end' . $this->r . $this->cR . '|i'
            =>
            '<?php if(\$$1) { ?>$2<?php }else{ ?>$3<?php }?>',
            /**
             * {.foreach arr}HTML1{.or}HTML2{.end}
             * <?php if(!empty($arr)&&is_array($arr)){foreach($arr as $k=>$v{) ?>HTML1<?php }}else{ ?＞HTML2<?php }?>
             */
            '|' . $this->cL . $this->l . '\.foreach\s+([a-z0-9_]+)' . $this->r . $this->cR . '([\w\W]*?)' . $this->cL . $this->l . '\.or' . $this->r . $this->cR . '([\w\W]*?)' . $this->cL . $this->l . '\.end' . $this->r . $this->cR . '|i'
            =>
            '<?php if(!empty(\$$1)||is_array(\$$1)){foreach(\$$1 as $k=>$v){ ?>$2<?php }}else{ ?>$3<?php } ?>'
        );

        $this->reg = array_merge($varReg, $usrReg);
    }



    public function assign($key ,$val)
    {
        $this->data[$key] = $val;
    }

    public function display($template)
    {
        //$people = array(array('bar'=>'hello'));
//        $foo = array('bar'=>'hello');
//        $num = 5 ;
//        $Debug = true;
        foreach ($this->data as $key => $val) {
            $$key = $val;
        }


        $templateFilePath = $this->templatePath . '/' . $template . $this->templateExtension;
        $cacheFilePath = $this->cachePath . '/' . $template . $this->templateExtension;
        $str = file_get_contents($templateFilePath);

        $str = preg_replace(array_keys($this->reg), array_values($this->reg), $str);

        file_put_contents($cacheFilePath, $str);

        include_once($cacheFilePath);

        //echo $templateFilePath;
        //echo file_get_contents($templateFilePath);
//        $cacheFilePath = $this->cachePath . '/' . $template . $this->templateExtension;
//        if($this->cacheSwitch)
//        {
//
            //$str = C::read($cacheFilePath);
            //if ($str === false)
            //{/** cache file not found */
//                self::makeMapping();
//                C::write(self::$cacheFileName, self::$mapping);
            //}
//        }
//        else
//        {
//            $str =
//        }
//        echo $this->cachePath . '/' . $template . $this->cacheExtension;
//        //echo $str;
    }


}